﻿//=======================================================
// 作者：LR
// 公司：广州旗博士科技有限公司
// 描述：工具人
// 创建时间：#CreateTime#
//=======================================================
using System.IO;
using UnityEngine;
using System.Text;


public class ResClass
{
    public StringBuilder builder = new StringBuilder();

    public ResClass(string folder,int order)
    {

        if (!Directory.Exists(Application.dataPath + "/Resources"))
        {
            Debug.LogError($"不存在文件夹 Resources");
            return;
        }

        DirectoryInfo directory = new DirectoryInfo(folder);

        if (directory == null)
        {
            Debug.LogError($"不存在文件夹{folder}");
            return;
        }


        var files      = directory.GetFiles();
        var directorys = directory.GetDirectories();
        var className = directory.Name == CreatUtil.res_FD ? "ResFolder" :CreatUtil.GetClassString(directory.Name);

        builder.Clear();
        builder.AppendLine($"  {CreatUtil.GetFormat(order)}//自动生成类，不能修改");
        builder.AppendLine($"  {CreatUtil.GetFormat(order)}public class {className}");
        builder.AppendLine($"  {CreatUtil.GetFormat(order)}{CreatUtil.LeftSymbol}");


        foreach (var item in files)
        {
            if (item.FullName.Contains(".meta"))
                continue;

            var outName         = Path.GetFileNameWithoutExtension(item.Name);
            var outFullName     = CreatUtil.SubstringSingle(item.FullName,CreatUtil.res_FD);
            var outFullNameSuf  = $"{CreatUtil.SubstringSingle(Path.GetDirectoryName(item.FullName), CreatUtil.res_FD)}/{outName}".TrimStart('/');

            builder.AppendLine(CreatUtil.SetStringField(outName, outFullNameSuf, order));
            builder.AppendLine(CreatUtil.SetStringFieldSuf(outName, outFullName, order));
        }

        foreach (var item in directorys)
        {
            if (item.FullName.Contains(".meta"))
                continue;

            var dirClass = CreatUtil.CreatResClass(CreatUtil.SubstringToAssetsPath(item.FullName), order + 2);
            builder.AppendLine($"{dirClass.builder}");
        }

        builder.AppendLine($"  {CreatUtil.GetFormat(order)}{CreatUtil.RightSymbol}");
    }
}

